package app.netant.bt.btcats;

import org.jsoup.select.Elements;

import app.netant.bt.MargnetDetail;
/**
 * BtCat - Detail处理
 * @author JiaJ
 *
 */
public class BtCatDetail extends MargnetDetail {

	public BtCatDetail(String netSite) {
		super(netSite);
	}
	
	private void analyseName(){
		Elements activePart = document.select("#wrapper #content #wall h2");
		if(activePart.size()>0){			
			this.entity.setMargnetName(activePart.get(0).text());
		}
	}
	
	private void analyseTags(){
		Elements tagsParts = document.select("#wrapper #content #wall .detail-table.detail-width tbody tr th");
		Elements valsParts = document.select("#wrapper #content #wall .detail-table.detail-width tbody tr td");
		
		for(int i=0;i<tagsParts.size();i++){
			String name = tagsParts.get(i).text();
			String value = valsParts.get(i).text();
			
			if("文件大小".equals(name)){
				this.entity.setMargnetSize(value);
			}
			
			else if("创建时间".equals(name)){
				this.entity.setMargnetDate(value);
			}
		}
	}
	
	private void analyseLink(){
		
		Elements elementParts = document.select("#wrapper #content #wall .detail-panel.detail-width .detail-panel.detail-width");
		for(int i=0;i<elementParts.size();i++){
			
			String tagName = elementParts.get(i).select(".panel-header").text();
			String tagValue = elementParts.get(i).select(".panel-body").text();
			
			if("磁力链接".equals(tagName)){
				tagValue = elementParts.get(i).select(".panel-body a").text();
				this.entity.setMargnetURL(tagValue);
			}
			
			else if("文件列表".equals(tagName)){
				
				Elements fileParts = elementParts.get(i).select(".panel-body ol li");
				tagValue = "";
				for(int j=0;j<fileParts.size();j++){
					
					String value = fileParts.get(j).text();
					if(value.indexOf("如果您看到此文件，请升级到BitComet(比特彗星)")>0){
						continue;
					}
					if(value.indexOf("please update to BitComet 0.85")>0){
						continue;
					}
					
					tagValue += value+"\n";
				}
				
				if(tagValue.length()>1300){
					tagValue = tagValue.substring(0,1300)+"...";
				}
				
				this.entity.setMargnetFile(tagValue);
				
			}

		}
		
		
	}

	@Override
	public void analyse() {
		
		this.analyseName();
		this.analyseLink();
		this.analyseTags();
		
	}

}
